﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql;
using System.Data;
using System.Diagnostics;

namespace document_manage.搜索文档
{
    /// <summary>
    /// Search.xaml 的交互逻辑
    /// </summary>
    public partial class Search : Window
    {
        public Search()
        {
            InitializeComponent();
        }

        MySQLConn con = new MySQLConn();            //连接数据库
    
        private void textBox_Search_TextChanged(object sender, TextChangedEventArgs e)
        {
          
            string text = "";
            text =  this.textBox_Search.Text;
            if (text != "")
            {
                Dispatcher.Invoke(() => dataGrid.ItemsSource = null);
                string sql = "select * from file where file_name regexp '" + text + "'";
                DataTable dt = new DataTable();         //新建DataTable类存放数据库输出结果
                dt = con.ExecuteQuery(sql);
                //判断空值
                if (dt != null)
                {
                    dataGrid.Items.Clear();
                    dataGrid.ItemsSource = dt.DefaultView;  //将datatable绑定到datagrid上
                }
                else MessageBox.Show("没有搜索的对象");
            }
          
        }//textBox_Search_TextChanged

        private void Click1(object sender, RoutedEventArgs e)
        {
            //获取选中行的某列的值
            DataRowView selectedElement = (DataRowView)dataGrid.SelectedItem;
            string name = selectedElement.Row[1].ToString(); //获取第一列的值,即name值
            string path = selectedElement.Row[2].ToString();
            string s = (path + "\\" + name);
            Process.Start(s);
        }

        private void Click2(object sender, RoutedEventArgs e)
        {
            //获取选中行的某列的值
            DataRowView selectedElement = (DataRowView)dataGrid.SelectedItem;
            string path = selectedElement.Row[2].ToString();
            //打开文件夹
            Process.Start(path);
        }



    }
}
